package com.yanqu.road.server.cmd.activity.carptrip;

import com.yanqu.road.entity.activity.carptrip.CarpTripConfig;
import com.yanqu.road.entity.activity.carptrip.config.CarpTripRoleInfo;
import com.yanqu.road.entity.activity.carptrip.data.UserCarpTripData;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.CarpTripProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.carptrip.CarpTripModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.carptrip.CarpTripMgr;
import com.yanqu.road.server.pb.CarpTripActivityPb;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_CARP_TRIP_GET_DATA, desc = "获取数据")
public class CarpTripGetUserDataCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        // 没有活动
        if (!ActivityMgr.activityInShowTime(CarpTripMgr.getActivityInfo()) || CarpTripMgr.getConfig() == null) {
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }

        CarpTripProto.CarpTripGetUserDataRespMsg.Builder respMsg = CarpTripProto.CarpTripGetUserDataRespMsg.newBuilder();
        CarpTripModule module = player.getModule(CarpTripModule.class);
        UserCarpTripData userData = module.getUserData();
        if(userData == null){
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }
        module.recoverUserEnergyPower();
        CarpTripConfig config = CarpTripMgr.getConfig();
        CarpTripRoleInfo roleInfo = config.getCarpTripRoleInfo(userData.getExp());
        CarpTripProto.CarpTripUserDataTemp.Builder userBuilder = CarpTripActivityPb.buildUserData(userData, roleInfo);
        CarpTripProto.CarpTripConfigSyncMsg.Builder configSync = CarpTripProto.CarpTripConfigSyncMsg.newBuilder();
        module.buildPassCellTempList(config, configSync, userData.getEvent().getPassId(), userData);
        respMsg.setRet(0);
        respMsg.setUserData(userBuilder);
        respMsg.addAllPass(configSync.getPassList());
        player.sendPacket(Protocol.U_CARP_TRIP_GET_DATA, respMsg);

    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_CARP_TRIP_GET_DATA, CarpTripProto.CarpTripGetUserDataRespMsg.newBuilder().setRet(ret));
        }
    }
}
